//
//  CQTitleImageTableViewCell.m
//  TSListDemo
//
//  Created by qian on 2020/11/18.
//  Copyright © 2020 dvlproad. All rights reserved.
//

#import "CQTitleImageTableViewCell.h"
#import <Masonry/Masonry.h>
#import <CJBaseUIKit/UIColor+CJHex.h>
#import "CQAvatarImageView.h"
#import "UIImage+CQCellBundle.h"

@interface CQTitleImageTableViewCell () {
    
}
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) CQAvatarImageView *resultImage;
@property (nonatomic, strong) UIImageView *goImageView;


@end

@implementation CQTitleImageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
        self.updateEnable = YES;
    }
    return self;
}

#pragma mark - Setter
- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (void)setResultImageUrl:(NSString *)resultImageUrl {
    _resultImageUrl = resultImageUrl;
    
    CQStatusImageModel *statusImageModel = [[CQStatusImageModel alloc] initWithImageUrl:resultImageUrl imageStatus:CQImageStatusActive];
    [self.resultImage updateImageWithNetImageModel:statusImageModel];
}
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
//    self.textField.placeholder = placeholder;
}

- (void)setHideIndicator:(BOOL)hideIndicator {
    _hideIndicator = hideIndicator;
    
    if (hideIndicator) {
//        self.accessoryType = UITableViewCellAccessoryNone;
        self.accessoryView.hidden = YES;
    } else {
//        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.accessoryView.hidden = NO;
    }
}

- (void)setUpdateEnable:(BOOL)updateEnable {
    _updateEnable = updateEnable;
    
    if (updateEnable) {
        self.hideIndicator = NO;
    } else {
        self.hideIndicator = YES;
    }
}

#pragma mark - Event
- (void)updateThemeType:(BOOL)isBlack {
    if (isBlack) {
        self.backgroundColor = CJColorFromHexString(@"#26262C");
        self.titleLabel.textColor = CJColorFromHexString(@"#6D6F76");
    } else {
        self.backgroundColor = CJColorFromHexString(@"#FFFFFF");
        self.titleLabel.textColor = CJColorFromHexString(@"#B6B7BA");
    }
}


#pragma mark - SetupViews
- (void)setupViews {
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = CJColorFromHexString(@"#B6B7BA");
    titleLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(26);
        make.top.equalTo(self);
        make.centerY.equalTo(self);
        make.width.equalTo(@46);
        make.height.greaterThanOrEqualTo(@60); // 为了自适应的时候，最小的cell高度不会小于指定值
    }];
    self.titleLabel = titleLabel;
    
    CQAvatarImageView *resultImage = [[CQAvatarImageView alloc] initWithFrame:CGRectZero bannedSize:CQBannedSizeSmall];
    resultImage.layer.cornerRadius = 8;
    resultImage.layer.masksToBounds = YES;
    [self addSubview:resultImage];
    [resultImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(88); // 不使用 titleLabel.mas_right 来约束，避免titleLabel长度不好把握而出现偏差
        make.width.height.equalTo(@34);
        make.centerY.equalTo(self);
    }];
    self.resultImage = resultImage;
    
    UIImageView *goImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    goImageView.image = [UIImage cell_nextArrowImage_default];
    self.accessoryView = goImageView;
//    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = CJColorFromHexStringAndAlpha(@"#0C101B", 0.05);
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(resultImage);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1);
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
